{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Apply Operations to an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: applyOperations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #对数组执行操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>nums</code> ，数组大小为 <code>n</code> ，且由 <strong>非负</strong> 整数组成。</p>\n",
    "\n",
    "<p>你需要对数组执行 <code>n - 1</code> 步操作，其中第 <code>i</code> 步操作（从 <strong>0</strong> 开始计数）要求对 <code>nums</code> 中第 <code>i</code> 个元素执行下述指令：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果 <code>nums[i] == nums[i + 1]</code> ，则 <code>nums[i]</code> 的值变成原来的 <code>2</code> 倍，<code>nums[i + 1]</code> 的值变成 <code>0</code> 。否则，跳过这步操作。</li>\n",
    "</ul>\n",
    "\n",
    "<p>在执行完 <strong>全部</strong> 操作后，将所有 <code>0</code> <strong>移动</strong> 到数组的 <strong>末尾</strong> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，数组 <code>[1,0,2,0,0,1]</code> 将所有 <code>0</code> 移动到末尾后变为 <code>[1,2,1,0,0,0]</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回结果数组。</p>\n",
    "\n",
    "<p><strong>注意</strong> 操作应当 <strong>依次有序</strong> 执行，而不是一次性全部执行。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,2,1,1,0]\n",
    "<strong>输出：</strong>[1,4,2,0,0,0]\n",
    "<strong>解释：</strong>执行以下操作：\n",
    "- i = 0: nums[0] 和 nums[1] 不相等，跳过这步操作。\n",
    "- i = 1: nums[1] 和 nums[2] 相等，nums[1] 的值变成原来的 2 倍，nums[2] 的值变成 0 。数组变成 [1,<em><strong>4</strong></em>,<em><strong>0</strong></em>,1,1,0] 。\n",
    "- i = 2: nums[2] 和 nums[3] 不相等，所以跳过这步操作。\n",
    "- i = 3: nums[3] 和 nums[4] 相等，nums[3] 的值变成原来的 2 倍，nums[4] 的值变成 0 。数组变成 [1,4,0,<em><strong>2</strong></em>,<em><strong>0</strong></em>,0] 。\n",
    "- i = 4: nums[4] 和 nums[5] 相等，nums[4] 的值变成原来的 2 倍，nums[5] 的值变成 0 。数组变成 [1,4,0,2,<em><strong>0</strong></em>,<em><strong>0</strong></em>] 。\n",
    "执行完所有操作后，将 0 全部移动到数组末尾，得到结果数组 [1,4,2,0,0,0] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [0,1]\n",
    "<strong>输出：</strong>[1,0]\n",
    "<strong>解释：</strong>无法执行任何操作，只需要将 0 移动到末尾。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 2000</code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [apply-operations-to-an-array](https://leetcode.cn/problems/apply-operations-to-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [apply-operations-to-an-array](https://leetcode.cn/problems/apply-operations-to-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,2,1,1,0]', '[0,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] <<= 1\n",
    "                nums[i + 1] = 0\n",
    "        ans = [0] * n \n",
    "        i = 0\n",
    "        for x in nums:\n",
    "            if x:\n",
    "                ans[i] = x\n",
    "                i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] = 2 * nums[i]\n",
    "                nums[i + 1] = 0\n",
    "        l = 0\n",
    "        for i in range(n):\n",
    "            if nums[i] != 0:\n",
    "                nums[l] = nums[i]\n",
    "                l += 1\n",
    "        for i in range(l,n):\n",
    "            nums[i] = 0\n",
    "        return nums\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i]*=2\n",
    "                nums[i+1]=0\n",
    "        # for i in range(len(nums)):\n",
    "        #     flag=1\n",
    "        #     if not flag:\n",
    "        #         return nums\n",
    "        #     elif nums[i]==0:\n",
    "        #         for j in range(i+1,len(nums)):\n",
    "        #             if nums[j]!=0:\n",
    "        #                 nums[i],nums[j]=nums[j],nums[i]\n",
    "        #                 break\n",
    "        #             if j==len(nums)-1 and nums[j]==0:\n",
    "        #                 flag=0\n",
    "        ans=[]\n",
    "        for i in nums:\n",
    "            if i:\n",
    "                ans.append(i)\n",
    "        while len(ans)<len(nums):\n",
    "            ans.append(0)\n",
    "        return ans\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if i + 1 < n and nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        for i in range(n-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "            if nums[i] != 0:\n",
    "                res.append(nums[i])\n",
    "        if nums[-1] != 0:\n",
    "            res.append(nums[-1])\n",
    "        n1 = len(res)\n",
    "        for _ in range(n-n1):\n",
    "            res.append(0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        l = last = 0\n",
    "        for i, n in enumerate(chain(nums, [0])):\n",
    "            if last:\n",
    "                if n == last:\n",
    "                    nums[l] = n*2\n",
    "                    last = 0\n",
    "                else:\n",
    "                    nums[l] = last\n",
    "                    last = n\n",
    "                l += 1\n",
    "            else:\n",
    "                last = n\n",
    "        for off in range(l, len(nums)):\n",
    "            nums[off] = 0\n",
    "        return nums\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        j = 0\n",
    "        for i in range(len(nums)):\n",
    "            if i < len(nums) - 1 and nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] = nums[i] * 2\n",
    "                nums[i+1] = 0\n",
    "        ans = [0] * n\n",
    "        index = 0\n",
    "        for j in nums:\n",
    "            if j != 0:\n",
    "                ans[index] = j\n",
    "                index += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        i=0\n",
    "        while i <n:\n",
    "            if i==n-1:\n",
    "                break\n",
    "            elif nums[i]==nums[i+1]:\n",
    "                nums[i]=nums[i]*2\n",
    "                nums[i+1]=0\n",
    "                i+=2\n",
    "            else:\n",
    "                i+=1\n",
    "        res=[x for x in nums if x!=0]\n",
    "        res.extend([0]*nums.count(0))\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "        ans = [0] * n \n",
    "        i = 0\n",
    "        for x in nums:\n",
    "            if x:\n",
    "                ans[i] = x\n",
    "                i += 1\n",
    "        return ans\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if  i+1 < n and nums[i] == nums[i + 1] :\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "        \n",
    "            if nums[i] != 0:\n",
    "                temp = nums[i]\n",
    "                nums[i] = nums[j]\n",
    "                nums[j] = temp\n",
    "                j += 1\n",
    "\n",
    "        return nums\n",
    "            \n",
    "\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "        return [i for i in nums if i != 0] + [i for i in nums if i == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i]*=2\n",
    "                nums[i+1]=0\n",
    "        ans=[0]*len(nums)\n",
    "        i=0\n",
    "        for num in nums:\n",
    "            if num==0:\n",
    "                continue\n",
    "            ans[i]=num\n",
    "            i+=1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        \n",
    "        for i in range(len(nums)-1) :\n",
    "            if nums[i] == nums[i+1] :\n",
    "                nums[i] = nums[i]+nums[i]\n",
    "                nums[i+1]=0\n",
    "        #print(nums)\n",
    "        i=0\n",
    "        for j in range(len(nums)) :\n",
    "            i=j\n",
    "            if nums[j] ==0 :\n",
    "                break\n",
    "        #print(i)\n",
    "        \n",
    "        for j in range(i+1,len(nums)) :\n",
    "            if nums[j] !=0 :\n",
    "                nums[i],nums[j]=nums[j],0\n",
    "                i+=1\n",
    "        #print(nums)\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def applyOperations(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        n = len(nums)\n",
    "        ret = [0]*n\n",
    "        j = 0 \n",
    "        for i in range(n-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2 \n",
    "                nums[i+1] = 0\n",
    "        for i in range(n):\n",
    "            if nums[i] != 0:\n",
    "                ret[j] = nums[i]\n",
    "                j += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if i+1<n and nums[i]==nums[i+1]:\n",
    "                nums[i] = 2*nums[i]\n",
    "                nums[i+1] = 0\n",
    "            if nums[i]!=0:\n",
    "                nums[i],nums[j] = nums[j],nums[i]\n",
    "                j += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if i + 1 < n and nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if i + 1 < n and nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i]=nums[i]*2\n",
    "                nums[i+1]=0\n",
    "        # print(nums)\n",
    "        lst=list()\n",
    "        for i in nums:\n",
    "            if i!=0:\n",
    "                lst.append(i)\n",
    "        for i in range(len(nums)-len(lst)):\n",
    "            lst.append(0)\n",
    "        return lst\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i]=nums[i]*2\n",
    "                nums[i+1]=0\n",
    "        nums=[i for i in nums if i!=0]+[i for i in nums if i==0]\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i]*=2\n",
    "                nums[i+1]=0\n",
    "            else:\n",
    "                continue\n",
    "        for j in nums:\n",
    "            if j==0:\n",
    "                nums.remove(j)\n",
    "                nums.append(0)\n",
    "        return nums        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "        return [x for x in nums if x != 0] + [x for x in nums if x == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i]*=2\n",
    "                nums[i+1]=0\n",
    "        # for i in range(len(nums)):\n",
    "        #     flag=1\n",
    "        #     if not flag:\n",
    "        #         return nums\n",
    "        #     elif nums[i]==0:\n",
    "        #         for j in range(i+1,len(nums)):\n",
    "        #             if nums[j]!=0:\n",
    "        #                 nums[i],nums[j]=nums[j],nums[i]\n",
    "        #                 break\n",
    "        #             if j==len(nums)-1 and nums[j]==0:\n",
    "        #                 flag=0\n",
    "        ans=[]\n",
    "        for i in nums:\n",
    "            if i:\n",
    "                ans.append(i)\n",
    "        while len(ans)<len(nums):\n",
    "            ans.append(0)\n",
    "        return ans\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        i=0\n",
    "        while i<n-1:\n",
    "            if nums[i]!=0 and nums[i]==nums[i+1]:\n",
    "                nums[i]*=2\n",
    "                nums[i+1]=0\n",
    "                i+=1\n",
    "            i+=1\n",
    "        ans=[i for i in nums if i!=0]\n",
    "        return ans+[0]*(n-len(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, a: List[int]) -> List[int]:\n",
    "        j=0\n",
    "        n=len(a)\n",
    "        for i in range(n-1):\n",
    "            if a[i]:\n",
    "                if a[i]==a[i+1]:\n",
    "                    a[i]*=2\n",
    "                    a[i+1]=0\n",
    "                a[j]=a[i]\n",
    "                j+=1\n",
    "        if a[-1]:\n",
    "            a[j]=a[-1]\n",
    "            j+=1\n",
    "        for i in range(j,n):\n",
    "            a[i]=0\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i]=nums[i]*2\n",
    "                nums[i+1]=0\n",
    "        \n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==0:\n",
    "                break\n",
    "        left=i\n",
    "        right=i+1\n",
    "\n",
    "        while right<len(nums):\n",
    "            while right<len(nums) and nums[right]==0:\n",
    "                right+=1\n",
    "            if right==len(nums):\n",
    "                break\n",
    "            nums[left]=nums[right]\n",
    "            nums[right]=0\n",
    "            while left<len(nums) and nums[left]!=0:\n",
    "                left+=1\n",
    "            if left==len(nums):\n",
    "                break\n",
    "\n",
    "        return nums\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(0, len(nums)-1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] = nums[i] * 2\n",
    "                nums[i + 1] = 0\n",
    "        j, k = 0, 0\n",
    "        while k <len(nums):\n",
    "            if nums[k] != 0:\n",
    "                nums[j] = nums[k]\n",
    "                if j != k:\n",
    "                    nums[k] = 0\n",
    "                j += 1\n",
    "            k += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "        k = j = 0\n",
    "        while j+k<n:\n",
    "            if nums[j] == 0:\n",
    "                nums.pop(j)\n",
    "                k +=1\n",
    "            else:\n",
    "                j+=1\n",
    "        return nums + [0]*k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        if n == 1 or n == 0:\n",
    "            return nums\n",
    "            \n",
    "        jet = [0] * n\n",
    "        j = 0\n",
    "\n",
    "        for i in range(n-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] = 2 * nums[i]\n",
    "                nums[i+1] = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            if nums[i] != 0:\n",
    "                jet[j] = nums[i]\n",
    "                j += 1\n",
    "        return jet        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums):\n",
    "        l  = len(nums)\n",
    "        left = []\n",
    "        right= []\n",
    "        n = -1\n",
    "        def l_or_r():\n",
    "            if n:\n",
    "                left.append(n)\n",
    "            else:\n",
    "                right.append(n)\n",
    "        for i in range(l-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] <<= 1\n",
    "                nums[i+1] = 0\n",
    "            n = nums[i]\n",
    "            l_or_r()\n",
    "        n = nums[-1]\n",
    "        l_or_r()\n",
    "        return left + right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        ln = len(nums)\n",
    "        for i in range(ln-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "        l1, l2 = [], []\n",
    "        for item in nums:\n",
    "            if item:\n",
    "                l1.append(item)\n",
    "            else:\n",
    "                l2.append(item)\n",
    "        nums = l1 + l2\n",
    "        \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums ):\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if i + 1 < n and nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        ln = len(nums)\n",
    "        j = 0\n",
    "        for i in range(ln):\n",
    "            if  i + 1 < ln and nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        i=0\n",
    "        while i<n-1:\n",
    "            for i in range(n-1):\n",
    "                if nums[i]==nums[i+1]:\n",
    "                    nums[i]=nums[i]*2\n",
    "                    nums[i+1]=0\n",
    "                    i+=2\n",
    "                else:\n",
    "                    i+=1\n",
    "            nums1 = [num for num in nums if num != 0]\n",
    "            count = len(nums1)\n",
    "            return nums1 + [0] * (n - count)\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# from typing import List\n",
    "\n",
    "# class Solution:\n",
    "#     def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "#         n = len(nums)\n",
    "#         i = 0\n",
    "        \n",
    "#         while i < n - 1:\n",
    "#             if nums[i] == nums[i + 1]:\n",
    "#                 nums[i] *= 2\n",
    "#                 nums[i + 1] = 0\n",
    "#                 i += 2  # 跳过下一个元素\n",
    "#             else:\n",
    "#                 i += 1\n",
    "        \n",
    "#         nums1 = [num for num in nums if num != 0]\n",
    "#         count = len(nums1)\n",
    "#         return nums1 + [0] * (n - count)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, a: List[int]) -> List[int]:\n",
    "        n = len(a)\n",
    "        for i in range (n-1):\n",
    "            if a[i] == a[i+1]:\n",
    "                a[i] *= 2\n",
    "                a[i+1] = 0\n",
    "        \n",
    "        b = [x for x in a if x]\n",
    "        b += [0]*(n - len(b))\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] = 2*nums[i]\n",
    "                nums[i+1] = 0 \n",
    "                i += 1\n",
    "            else:\n",
    "                i+=1\n",
    "        num=[]\n",
    "        count = 0\n",
    "        for i in nums:\n",
    "            if i !=0:\n",
    "                num.append(i)\n",
    "            else :\n",
    "                count+=1\n",
    "        for j in range(count):\n",
    "            num.append(0)\n",
    "        nums = num\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if i + 1 < n and nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] += nums[i]\n",
    "                nums[i + 1] = 0\n",
    "        c, ans = nums.count(0), []\n",
    "        ans = [n for n in nums if n != 0]\n",
    "        return ans + [0] * c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i] = 2*nums[i]\n",
    "                nums[i+1]=0\n",
    "        ans = []\n",
    "        count = 0\n",
    "        for j in nums:\n",
    "            if j!=0:\n",
    "                ans.append(j)\n",
    "            else:\n",
    "                count +=1\n",
    "        while count>0:\n",
    "            ans.append(0)\n",
    "            count -=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] = nums[i] * 2\n",
    "                nums[i+1] = 0\n",
    "        \n",
    "        res = []\n",
    "        for j in range(n):\n",
    "            if nums[j] != 0:\n",
    "                res.append(nums[j])\n",
    "        \n",
    "        res += [0] * (n-len(res))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n= len(nums)\n",
    "        j= 0\n",
    "        for i in range(n):\n",
    "            if i+1< n and nums[i]== nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+ 1]= 0\n",
    "            if nums[i]!= 0:\n",
    "                nums[i], nums[j]= nums[j], nums[i]\n",
    "                j +=1\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        ln = len(nums)\n",
    "        for i in range(ln-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "        l1, l2 = [], []\n",
    "        for item in nums:\n",
    "            if item:\n",
    "                l1.append(item)\n",
    "            else:\n",
    "                l2.append(item)\n",
    "        nums = l1 + l2\n",
    "        \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] <<= 1\n",
    "                nums[i + 1] = 0\n",
    "        ans = [0] * n\n",
    "        i = 0\n",
    "        for x in nums:\n",
    "            if x:\n",
    "                ans[i] = x\n",
    "                i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: list[int]) -> list[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if(nums[i]==nums[i+1]):\n",
    "                nums[i] = nums[i] * 2\n",
    "                nums[i+1]=0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==0:\n",
    "                nums.remove(0)\n",
    "                nums.append(0)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] += nums[i]\n",
    "                nums[i + 1] = 0\n",
    "        return [n for n in nums if n != 0] + [0] * nums.count(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        return_val = []\n",
    "        idx = 0\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i] == 0:\n",
    "                return_val.append(0)\n",
    "            elif nums[i] == nums[i + 1]:\n",
    "                nums[i + 1] = 0\n",
    "                return_val.insert(idx, 2 * nums[i])\n",
    "                idx += 1\n",
    "            else:\n",
    "                return_val.insert(idx, nums[i])\n",
    "                idx += 1\n",
    "        return_val.insert(idx, nums[-1]) if nums[-1] != 0 else return_val.append(0)\n",
    "        return return_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "        return [x for x in nums if x != 0] + [x for x in nums if x == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] <<= 1\n",
    "                nums[i + 1] = 0\n",
    "        i = j = 0\n",
    "        while j < n:\n",
    "            while j < n and nums[j] == 0:\n",
    "                j += 1\n",
    "            if j == n:\n",
    "                break\n",
    "            nums[i], nums[j] = nums[j], nums[i]\n",
    "            i += 1\n",
    "            j += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        out = []\n",
    "        i = 0\n",
    "        while(i<len(nums)):\n",
    "            if  i+1<len(nums):\n",
    "                if nums[i] == nums [i+1] and nums[i] != 0:\n",
    "                    out.append(2*nums[i])\n",
    "                    i += 2\n",
    "                else:\n",
    "                    if nums[i] != 0:\n",
    "                        out.append(nums[i])\n",
    "                    i += 1\n",
    "            else:\n",
    "                if nums[i] != 0:\n",
    "                    out.append(nums[i])\n",
    "                i += 1\n",
    "        out = out + (len(nums)-len(out))*[0]\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i]*=2\n",
    "                nums[i+1]=0\n",
    "            else:\n",
    "                continue\n",
    "        for j in nums:\n",
    "            if j==0:\n",
    "                nums.remove(j)\n",
    "                nums.append(0)\n",
    "        return nums        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        c, i, ln = 0, 0, len(nums)\n",
    "        ans = []\n",
    "        while i < ln - 1:\n",
    "            if nums[i] != 0:\n",
    "                if nums[i] == nums[i + 1]:\n",
    "                    ans.append(nums[i] * 2)\n",
    "                    nums[i + 1] = 0\n",
    "                    i += 1\n",
    "                else:\n",
    "                    ans.append(nums[i])\n",
    "            i += 1\n",
    "        if nums[-1] == 0:\n",
    "            return ans + [0] * nums.count(0)\n",
    "        else:\n",
    "            return ans + [nums[-1]] + [0] * nums.count(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = []\n",
    "        for i in range(n - 1):\n",
    "            if nums[i] == 0:\n",
    "                continue\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            ans.append(nums[i])\n",
    "        if nums[-1] != 0:\n",
    "            ans.append(nums[-1])\n",
    "        return ans + [0] * (n - len(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i]*=2\n",
    "                nums[i+1]=0\n",
    "        List=[]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]!=0:\n",
    "                List.append(nums[i])\n",
    "        lenght=len(nums)-len(List)\n",
    "        return List+[0]*lenght\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if i+1<n and nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i],nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for num in nums:\n",
    "            if num == 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                ans.append(num)\n",
    "        return ans+[0]*cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        idx = 0\n",
    "        n = len(nums)\n",
    "        for i in range(n - 1) :\n",
    "            if nums[i] :\n",
    "                if nums[i] == nums[i + 1] :\n",
    "                    nums[i] *= 2\n",
    "                    nums[i + 1] = 0\n",
    "                nums[idx] = nums[i]\n",
    "                idx += 1\n",
    "        if nums[-1] :\n",
    "            nums[idx] = nums[-1]\n",
    "            idx += 1\n",
    "        for i in range(idx, n) :\n",
    "            nums[i] = 0\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "nums = [1,2,2,1,1,0]\n",
    "nums = [0,1]\n",
    "nums = [9, 5, 4, 0, 0, 0, 2, 0]\n",
    "from typing import List\n",
    "class Solution:\n",
    "    # @lambda _ : print(_(_, nums))\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        Length = len(nums)\n",
    "        x = 0\n",
    "        for i in range(Length):\n",
    "            if i + 1 < Length and nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            \n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[x] = nums[x], nums[i]\n",
    "                x += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "            else:\n",
    "                continue\n",
    "        ans = [0] * len(nums)\n",
    "        j = 0\n",
    "        for x in nums:\n",
    "            if x > 0:\n",
    "                ans[j] = x\n",
    "                j += 1\n",
    "        return ans\n",
    "            \n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "\n",
    "        res = []\n",
    "\n",
    "        i = 0\n",
    "\n",
    "        while i < len(nums) - 1:\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i], nums[i + 1] = nums[i] * 2, 0\n",
    "            i += 1\n",
    "        \n",
    "        for j in nums:\n",
    "            if j != 0:\n",
    "                res.append(j)\n",
    "\n",
    "        while len(res) < len(nums):\n",
    "            res.append(0)\n",
    "\n",
    "        return res\n",
    "        print(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] += nums[i]\n",
    "                nums[i + 1] = 0\n",
    "        c, i, ln = 0, 0, len(nums)\n",
    "        while i < ln and c < ln:\n",
    "            if nums[i] == 0:\n",
    "                nums.append(nums.pop(i))\n",
    "                c += 1\n",
    "                continue\n",
    "            i += 1\n",
    "            c += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        i = 0\n",
    "        j = 0\n",
    "        count = 0\n",
    "        while i < len(nums) - 1:\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "                i += 1\n",
    "            i += 1\n",
    "        \n",
    "        while j + count < len(nums):\n",
    "            if nums[j] == 0:\n",
    "                nums.remove(nums[j])\n",
    "                nums.append(0)\n",
    "                count += 1\n",
    "            else:\n",
    "                j += 1\n",
    "\n",
    "        return nums\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, a: List[int]) -> List[int]:\n",
    "        n = len(a)\n",
    "        j = 0\n",
    "        for i in range (n-1):\n",
    "            if a[i] == 0: continue\n",
    "            if a[i] == a[i+1]:\n",
    "                a[i] *= 2\n",
    "                a[i+1] = 0\n",
    "            a[j] = a[i]\n",
    "            j += 1\n",
    "        if a[-1]:\n",
    "            a[j] =a[-1]\n",
    "            j += 1\n",
    "        for i in range(j,n):\n",
    "            a[i] = 0 \n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        ln = len(nums)\n",
    "        for i in range(ln - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "        l,r = 0, 0\n",
    "        while r < ln:\n",
    "            if nums[r] != 0:\n",
    "                nums[l], nums[r] = nums[r], nums[l]\n",
    "                l += 1\n",
    "            r += 1\n",
    "        return nums\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        i=0\n",
    "        for s in range(len(nums)-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i]*=2\n",
    "                nums[i+1]=0\n",
    "            if not nums[i]:\n",
    "                nums.append(nums.pop(i))\n",
    "            else:\n",
    "                i+=1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *=2\n",
    "                nums[i+1] = 0 \n",
    "        \n",
    "        i = 0 \n",
    "        j = -1\n",
    "        while i < n:\n",
    "            if nums[i] == 0:\n",
    "                if j == -1:\n",
    "                    j = i+1\n",
    "                if j > n-1:\n",
    "                    break\n",
    "                while j < n:\n",
    "                    if nums[j] !=0:\n",
    "                        nums[i],nums[j] = nums[j],nums[i]\n",
    "                        j += 1\n",
    "                        break \n",
    "                    j += 1\n",
    "            i += 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        i = 0\n",
    "        cnt = 0\n",
    "        l = len(nums)\n",
    "        while i < l - 1:\n",
    "            if nums[i] == nums[i+1] and nums[i] != 0:\n",
    "                ans.append(nums[i]*2)\n",
    "                i += 1\n",
    "                cnt += 1\n",
    "            elif nums[i] !=0:\n",
    "                ans.append(nums[i])\n",
    "            elif nums[i] == 0:\n",
    "                cnt += 1\n",
    "            i += 1\n",
    "        if nums[l-1] == nums[l-2] and nums[i-1] != 0:\n",
    "            pass\n",
    "        else:\n",
    "            ans.append(nums[l-1])\n",
    "        ans += [0]*cnt\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i]  == nums[i + 1]:\n",
    "                nums[i] <<= 1\n",
    "                nums[i + 1] = 0\n",
    "        cnt = 0\n",
    "        ret = []\n",
    "        for x in nums:\n",
    "            if x:\n",
    "                ret.append(x)\n",
    "            else:\n",
    "                cnt += 1\n",
    "        return ret + [0] * cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i] == nums[i-1]:\n",
    "                nums[i-1] = 2*nums[i-1]\n",
    "                nums[i] = 0\n",
    "            else:\n",
    "                continue\n",
    "        count = 0\n",
    "        list1 = []\n",
    "        for j in nums:\n",
    "            if j == 0:\n",
    "                count +=1\n",
    "            else:\n",
    "                list1.append(j)\n",
    "        for k in range(count):\n",
    "            list1.append(0)\n",
    "        return list1\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "        n = nums.count(0)\n",
    "        while 0 in nums:\n",
    "            nums.remove(0)\n",
    "        return nums+[0]*n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if i + 1 < n and nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] = 2 * nums[i]\n",
    "                nums[i + 1] = 0\n",
    "        left = 0 \n",
    "        right = 0\n",
    "        vaild = True\n",
    "        while right < n:\n",
    "            if nums[left] == 0:\n",
    "                print(n, right)\n",
    "                while nums[right] == 0:\n",
    "                    right += 1\n",
    "                    if right >= n:\n",
    "                        vaild = False\n",
    "                        break\n",
    "                if not vaild:\n",
    "                    break\n",
    "                nums[left] = nums[right]\n",
    "                nums[right] = 0\n",
    "                left += 1\n",
    "                right += 1\n",
    "            else:\n",
    "                left += 1\n",
    "                right = left + 1\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "        \n",
    "        left, right = 0, 0\n",
    "\n",
    "        while right < len(nums):\n",
    "            if nums[right] != 0:\n",
    "                nums[right], nums[left] = nums[left], nums[right]\n",
    "                left += 1\n",
    "            #left += 1\n",
    "            right += 1\n",
    "             \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(0,len(nums)-1):\n",
    "            if nums[i] == nums[i+1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i+1] = 0\n",
    "        zeros = 0\n",
    "        result = []\n",
    "        for j in nums:\n",
    "            if j != 0:\n",
    "                result.append(j)\n",
    "            else:\n",
    "                zeros += 1\n",
    "        while zeros > 0:\n",
    "            result.append(0)\n",
    "            zeros -= 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# # class Solution: ### error 10/48--pass\n",
    "# #     def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "# #         n=len(nums)\n",
    "# #         for i in range(n-1):\n",
    "# #             if nums[i]==nums[i+1]:\n",
    "# #                 nums[i],nums[i+1]=2*nums[i],0 \n",
    "# #         j=n-1\n",
    "# #         for i in range(n):  ### error\n",
    "# #             # if i==j:\n",
    "# #             #     break\n",
    "# #             if nums[i]==0:\n",
    "# #                 nums[i:j]=nums[i+1:j+1]\n",
    "# #                 nums[j]=0\n",
    "# #                 j-=1\n",
    "# #             if i==j:\n",
    "# #                 break\n",
    "# #         return nums\n",
    "\n",
    "# ## my:模拟+双指针       \n",
    "# class Solution:\n",
    "#     def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "#         n=len(nums)\n",
    "#         for i in range(n-1):\n",
    "#             if nums[i]==nums[i+1]:\n",
    "#                 nums[i],nums[i+1]=2*nums[i],0 \n",
    "\n",
    "#         i,j=0,0\n",
    "#         while i<n and j<n:\n",
    "#             if nums[i]==0:\n",
    "#                 j=i if j<i else j\n",
    "#                 while j<n and nums[j]==0:\n",
    "#                     j+=1\n",
    "#                 if j<n:\n",
    "#                     nums[i],nums[j]=nums[j],nums[i]\n",
    "#                     j+=1\n",
    "#             i+=1\n",
    "#         return nums\n",
    "\n",
    "### 官码：模拟\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        j = 0\n",
    "        for i in range(n):\n",
    "            if i + 1 < n and nums[i] == nums[i + 1]:\n",
    "                nums[i] *= 2\n",
    "                nums[i + 1] = 0\n",
    "            if nums[i] != 0:\n",
    "                nums[i], nums[j] = nums[j], nums[i]\n",
    "                j += 1\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def applyOperations(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i]==nums[i+1]:\n",
    "                nums[i]*=2\n",
    "                nums[i+1]=0\n",
    "        ret=[]\n",
    "        for i in nums:\n",
    "            if i:\n",
    "                ret.append(i)\n",
    "        ret+=[0]*(n-len(ret))\n",
    "        return ret"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
